<template>
	<view class="content">

		<view class="tabList">
			<view class="li" :class="tabActive == index ? 'tabActive':''" v-for="(item,index) in tabList"
				@click="tabChange(index)">{{ item.text }}</view>
		</view>
		<view class="formCon" v-if="tabActive == 0">
			<view class="li_2">
				<textarea v-model="goutongText" style="width: 100%;padding: 20rpx;" type="text" placeholder="沟通详情"></textarea>
			</view>
		</view>
		<view class="formConImg" v-if="tabActive == 1">
			<view class="li" v-for="(item,index) in lists">
				<view class="text">{{ item.text }}</view>
				<view class="time">口述时间：{{ item.create_time }}</view>
			</view>
            <view class="empty" v-if="lists.length == 0">暂无沟通记录~</view>
		</view>
		<button class="btn" v-if="tabActive == 0" @click="saveFormData">保存</button>
	</view>
</template>

<script>
	import {
		list_kehugoutongjilu,
		add_kehugoutongjilu,
		put_kehu,
		list_kehu
	} from "@/utils/api.js";
	import {
		onLoad,
		onReachBottom,
		onPullDownRefresh
	} from '@dcloudio/uni-app'
	export default {
		data() {
			return {
				title: 'Hello',
				tabList: [{
						text: '新增沟通'
					},
					{
						text: '沟通记录'
					}
				],
				tabActive: 0,
				tops: 0,
				height: 0,
				// 数据列表
				lists: [],
				// 分页参数
				page: 1,
				pageSize: 10,
				count: '',
				kehu_id:'',
				formData:'',
				goutongText:''
			}
		},
		onLoad(options) {
			this.kehu_id = options.kehu_id
			if (this.kehu_id) {
				this.getIinfo()
			}
			 this.getList(1)
			uni.getSystemInfo({

				success: (e) => {

					this.tops = e.statusBarHeight;

					let custom = uni.getMenuButtonBoundingClientRect();

					this.height = custom.height + (custom.top - e.statusBarHeight) * 2;

				}

			})
           
		},
		methods: {
			onPullDownRefresh() {
				// 处理下拉刷新的逻辑
				console.log('下拉刷新');
				this.page = 1
				this.getList(1)
			},
			
			onReachBottom() {
				// 处理下拉加载更多的逻辑
				console.log('下拉加载更多');
				this.page++;
				// 模拟加载更多数据
				this.getList();
			},
			goBack() {
				uni.navigateBack()
			},
			tabChange(index) {
				this.tabActive = index
			},
			history() {
				uni.navigateTo({
					url: '/pages/customerHistory/index'
				})
			},
			async getIinfo(){
				let that = this
				let data = {
					user_id:uni.getStorageSync('userInfo').user_id,
					kehu_id:that.kehu_id
				}
				let res = await list_kehu(data,that.kehu_id);
				
					
				that.formData = res.results
				console.log('客户信息',that.formData)
			},
			// 列表
			async getList(type) {
				let that = this
				let data = {
					page: that.page,
					size: 20,
					kehu_id:that.kehu_id,
					user_id: uni.getStorageSync('userInfo').user_id
				}
				let res = await list_kehugoutongjilu(data);
			
				if (res.code == 0) {
					that.count = res.count
					console.log('沟通列表===', res.results)
					if (type == 1) {
						that.lists = res.results
					} else {
						that.lists.concat(res.results)
					}
			
					// 停止下拉刷新动画
					uni.stopPullDownRefresh();
				}
			
			},
			async saveFormData() {
				let that = this
				let data={
					  text: that.goutongText,
					  kehu: that.kehu_id
				}
				let res = await add_kehugoutongjilu(data, that.kehu_id)
				if (res.code == 0) {
					uni.showToast({
						title: '添加成功',
						//icon: 'none',
						duration: 2000
					});
					that.goutongText = ''
					that.page = 1
					that.tabActive = 1
					that.getList(1)
				}
			},

		}
	}
</script>

<style>
	.tabList {
		width: 90%;
		height: 80rpx;
		line-height: 80rpx;
		border: 1rpx solid #008BDE;
		margin: auto;
		display: flex;
	}

	.tabList .li {
		width: 50%;
		text-align: center;
		color: #008BDE;
	}

	.tabActive {
		background-color: #008BDE;
		color: white !important;
	}

	.formCon {
		width: 90%;
		margin: auto;
		margin-top: 50rpx;
	}

	.formCon .li {
		width: 100%;
		height: 80rpx;
		display: flex;
		justify-content: space-between;
		margin-top: 20rpx;
		
	}

	.formCon .li input {
		width: 44%;
		height: 80rpx;
		border-radius: 10rpx;
		padding-left: 20rpx;
		border: 1rpx solid #ccc;
		font-size: 26rpx;
	}

	.formCon .li .picker-box {
		width: 44%;
		height: 80rpx;
		border-radius: 10rpx;
		padding-left: 20rpx;
		border: 1rpx solid #ccc;
		font-size: 26rpx;
		position: relative;
	}

	.formCon .li .picker-box view {
		margin-top: 22rpx;
	}

	.formCon .li .picker-box image {
		width: 50rpx;
		height: 50rpx;
		position: absolute;
		right: 20rpx;
		top: 18rpx;
	}

	.formCon .li_1 {
		display: flex;
		margin-top: 20rpx;
	}

	.formCon .li_1 input {
		width: 86%;
		height: 80rpx;
		border-radius: 10rpx;
		padding-left: 20rpx;
		border: 1rpx solid #ccc;
		font-size: 26rpx;
	}

	.formCon .li_1 image {
		width: 60rpx;
		height: 60rpx;
		margin-left: 20rpx;
		margin-top: 14rpx;
	}

	.formCon .li_2 {
		display: flex;
		margin-top: 20rpx;
		border: 1rpx solid #ccc;
		border-radius: 10rpx;
	}

	.formCon .li_2 input {
		width: 86%;
		height: 80rpx;
		border-radius: 10rpx;
		padding-left: 20rpx;
		font-size: 26rpx;
	}

	.formCon .li_2 image {
		width: 60rpx;
		height: 60rpx;
		margin-right: 20rpx;
		margin-top: 14rpx;
	}

	.addOther {
		width: 400rpx;
		height: 80rpx;
		line-height: 80rpx;
		background: #008BDE;
		color: white;
		display: flex;
		justify-content: space-between;
		margin-top: 20rpx;
	}

	.addOther view {
		margin-left: 50rpx;
	}

	.addOther image {
		width: 50rpx;
		height: 50rpx;
		margin-top: 13rpx;
		margin-right: 50rpx;
	}

	.formConImg {
		width: 90%;
		margin: auto;
		margin-top: 30rpx;
	}

	.formConImg .title {
		display: flex;
	}

	.formConImg .title .text {
		margin-left: 20rpx;
	}

	.formConImg .li {
		margin-top: 20rpx;
		font-size: 28rpx;
		line-height: 50rpx;
		border-bottom: 1rpx solid #f5f5f5;
		padding-bottom: 20rpx;
	}

	.formConImg .li .time {
		color: #999;
	}

	.imgList {
		margin-top: 20rpx;
	}

	/deep/ .icon-add {
		width: 50rpx;
	}

	.btn {
		width: 80%;
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 80rpx;
		color: white;
		margin: auto;
		background-color: #008BDE;
		margin-top: 50rpx;
		font-size: 26rpx;
	}
	.empty{
	   width: 100%;
	   height: 30px;
	   text-align: center;
	   position: absolute;
	   bottom: 0;
	   top: 0;
	   margin: auto;
	}
</style>